﻿using AutoMapper;
using B.S.DataBase.MyProject.ErrorCode;
using MediatR;
using S.Y.Passport.Domain.溯源;
using S.Y.Passport.ErroeCode;
using S.Y.Passport.Infacet;
using S.Y.Passport.Read.API.Applications.Commands;

namespace S.Y.Passport.Read.API.Applications.CommandHandler
{
    public class GoodsUpdateCommandHandler : IRequestHandler<GoodsUpdateCommand, SyResult<object>>
    {
        private readonly IBaseRepo<GoodsInfo> goodsInfoRepo;
        private readonly IMapper mapper;

        public GoodsUpdateCommandHandler(IBaseRepo<GoodsInfo>goodsInfoRepo,IMapper mapper)
        {
            this.goodsInfoRepo = goodsInfoRepo;
            this.mapper = mapper;
        }
        public Task<SyResult<object>> Handle(GoodsUpdateCommand request, CancellationToken cancellationToken)
        {
            SyResult<object> result = new SyResult<object>();

            var getObj = goodsInfoRepo.GetAll().FirstOrDefault(x => x.GooodId == request.GooodId);
            getObj.GoodName = request.GoodName;
            getObj.Manufactory = request.Manufactory;
            getObj.Validuntil = request.Validuntil;
            getObj.Licence = request.Licence;
            getObj.Factory = request.Factory;
            getObj.CreateName = "张三";
            getObj.CreateTime = DateTime.Now;
            getObj.UpdateName = "张三";
            getObj.UpdateTime = DateTime.Now;

            //var model = mapper.Map<User>(request);
            var obj = goodsInfoRepo.Update(getObj);
            if (obj > 0)
            {
                result.Code = SyCode.更新成功;
                result.Msg = SyCode.更新成功.ToString();
                result.Data = obj;
            }
            else
            {
                result.Code = SyCode.更新失败;
                result.Msg = SyCode.更新失败.ToString();
            }
            return Task.FromResult(result);
        }
    }
}
